package org.actions.admin;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.displaytag.tags.TableTagParameters;
import org.displaytag.util.ParamEncoder;
import org.util.SportsmenDAO;
import org.util.EventDAO;
import org.util.Constants;
import org.util.GeneralDAO;

/**
 *
 * @author GeorgeMavchun
 */
public class ListAction extends ActionSupport {

    private List result;
    private Long totalCount;

    @Override
    public String execute() {
        return SUCCESS;
    }

    public String profiles() {
        Map parameters = (Map) ActionContext.getContext().get(ActionContext.PARAMETERS);
        int startNumber = getStartNumber(parameters);
        String sortColumn = getSortColumn(parameters);
        String sortOrder = getSortOrder(parameters);

        try {
            result = SportsmenDAO.getAllProfilesPartial(Constants.PAGE_SIZE, startNumber, sortColumn, sortOrder);
            totalCount = GeneralDAO.getTotalCount(SportsmenDAO.EMPTY_SPORTSMAN_PROFILE);
        } catch (Exception ex) {
            Logger.getLogger(SportsmenDAO.class.getName()).log(Level.SEVERE, null, ex);
            result = null;
            totalCount = new Long(0);
        }
        return "profiles";
    }

    public String events() {
        Map parameters = (Map) ActionContext.getContext().get(ActionContext.PARAMETERS);
        int startNumber = getStartNumber(parameters);
        String sortColumn = getSortColumn(parameters);
        String sortOrder = getSortOrder(parameters);

        try {
            result = EventDAO.getAllEventsPartial(Constants.PAGE_SIZE, startNumber, sortColumn, sortOrder);
            totalCount = GeneralDAO.getTotalCount(EventDAO.EMPTY_EVENT);
        } catch (Exception ex) {
            Logger.getLogger(SportsmenDAO.class.getName()).log(Level.SEVERE, null, ex);
            result = null;
            totalCount = new Long(0);
        }
        return "events";
    }

    public List getProfiles() {
        return result;
    }

    public List getEvents() {
        return result;
    }

    public Integer getTotalCount() {
        return totalCount.intValue();
    }

    private String getSortColumn(Map parameters) {
        String sortAttr = new ParamEncoder("allEvents").encodeParameterName(TableTagParameters.PARAMETER_SORT);
        try {
            String[] page = (String[]) parameters.get(sortAttr);
            return page[0];
        } catch (Exception e) {
            return "ID";
        }
    }

    private String getSortOrder(Map parameters) {
        String orderAttr = new ParamEncoder("allEvents").encodeParameterName(TableTagParameters.PARAMETER_ORDER);
        try {
            String[] page = (String[]) parameters.get(orderAttr);
            int key = Integer.parseInt(page[0]);
            if (key == 2) {
                return "DESC";
            }
        } catch (Exception e) {
            // do nothing
        }
        return "ASC";
    }

    private int getStartNumber(Map parameters) {
        String pageAttr = new ParamEncoder("allEvents").encodeParameterName(TableTagParameters.PARAMETER_PAGE);
        try {
            String[] page = (String[]) parameters.get(pageAttr);
            return (Integer.parseInt(page[0]) - 1) * Constants.PAGE_SIZE;
        } catch (Exception e) {
            return 0;
        }
    }
}
